package com.hontech.bvapp2.ui.parts.setting

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.Button
import android.widget.FrameLayout
import com.hontech.bvapp2.R
import com.hontech.bvapp2.device.Address
import com.hontech.bvapp2.device.DeviceConn
import com.hontech.bvapp2.ui.dialog.runProg
import com.hontech.bvapp2.ui.view.LabelSpinner
import com.hontech.bvapp2.utils.onClick
import com.hontech.bvapp2.utils.stringsById

class PingParts (context: Context, attrs: AttributeSet) : FrameLayout(context, attrs) {

    init {
        val view = LayoutInflater.from(context).inflate(R.layout.parts_setting_ping, this)
        val sp = view.findViewById<LabelSpinner>(R.id.id_ping_sp)
        val btnPing = view.findViewById<Button>(R.id.id_ping_btn_ping)
        val btnPingAll = view.findViewById<Button>(R.id.id_ping_btn_ping_all)

        btnPing.onClick {
            val cp = sp.selectText()
            runProg("Ping", "Ping$cp") {
                DeviceConn.ping( Address.getAddress(cp) )
            }
        }

        btnPingAll.onClick {
            val cps = stringsById(R.array.cps)
            runProg("Ping所有", "") {
                for (cp in cps) {
                    it.setMsg("Ping${cp}")
                    DeviceConn.ping(Address.getAddress(cp))
                }
            }
        }
    }
}












